package com.ybkj.daijia.server.event.listener.errand;

import com.ybkj.daijia.PushDataV4;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiEmployV4;
import com.ybkj.daijia.api.jpush.JiGuangEmployV4;
import com.ybkj.daijia.server.errand.OrderUp;
import com.ybkj.daijia.server.event.model.errand.PaotuiOrderPaidanUpEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.service.AreaService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class PaotuiOrderPaidanUpEventListener implements
    ApplicationListener<PaotuiOrderPaidanUpEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(PaotuiOrderPaidanUpEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private SmsService smsService;

    @Override
    @Async
    public void onApplicationEvent(PaotuiOrderPaidanUpEvent event) {

        OrderUp order = (OrderUp) event.getSource();
        Driver driver = event.getDriver();

        logger.debug("order paotuipaidan event:{}", order.getOrderNo());

        //发送短信
        SmsTemplet templet = smsService.findByAlias(driver.getCompanyId(), SmsType.paiPaotuiOrder);
        Company driverCompany = companyService.findOne(driver.getCompanyId());
        Company finalCompany = driverCompany;
        Setting setting = settingUtils.get();
        Company main = companyService.getMain();

        if (!driverCompany.equals(main.getId()) && setting.isSmsMainCompany()) {
            finalCompany = main;
        }

        if (null != templet && StringUtils.isNotBlank(templet.getContent())) {
//			try {
//				DateFormat format = new SimpleDateFormat("MM-dd HH:mm");
//				String serverTime=format.format(order.getStartTime()!=null?order.getStartTime():order.getCreated());
//				smsService.sendSms(driver.getPhone(),"paiPaotuiOrder",  "{\"订单号\":\""+"***"+order.getOrderNo().substring(4)+"\",\"客户姓名\":\""+order.getPassengerName()+"\",\"客户电话\":\""+order.getPassengerPhone()+"\",\"预约时间\":\""+serverTime+"\",\"预约地点\":\""+order.getStartAddress()+"\"}", finalCompany.getId(), 0, finalCompany.getSignName());
//			} catch (Exception e) {
//				logger.error("跑腿派单短信  发送短信发生异常：{}",e.getMessage());
//			}
        }

        try {

            //发送设备推送
            PushDataV4 pushDataV4 = new PushDataV4();
            pushDataV4.setCode("0X03");
            pushDataV4.setData(order.getId());
            pushDataV4.setType("0X02");
            pushDataV4.setSound("beep_3");
            Map<String, String> extra = new HashMap<String, String>();
            extra.put("content", "您有新的工单啦,单号:" + order.getOrderNo() + ",赶快打开服务人员端接单吧");
            pushDataV4.setExtra(extra);

            boolean getuiOnline = false;
            if (StringUtils.isNotBlank(driver.getUserID())) {
                getuiOnline = GeTuiEmployV4.getClientOnline(driver.getUserID(), settingUtils.get());
            }

            if (getuiOnline) {
                GeTuiEmployV4.push(driver, pushDataV4, settingUtils.get());
            } else {
                if (StringUtils.isNotBlank(driver.getRegistrationId())) {
                    JiGuangEmployV4.push(driver, pushDataV4, settingUtils.get());
                }
            }

        } catch (IOException e) {
            logger.debug("push error : " + e.getMessage());
        }


    }


}
